package com.sandking.db.dao;

import javax.sql.DataSource;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.QueryRunner;
import com.sandking.config.SK_Config;
import java.sql.Connection;
import com.sandking.metadata.jdbc.SK_Query;
import java.util.Map;
import java.util.List;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.handlers.MapListHandler;
import com.sandking.db.bean.Config;

public class ConfigDao {
	public static Config insert(Config config){
		Connection conn = SK_Config.getConnection();
		return insert(config,conn);
	}
	
	public static Config insert(Config config,Connection conn){
		return insert(config,conn,Config.TABLENAME);
	}
	
	public static Config insert(Config config,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insert(config,conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Config insert(Config config,String tableName){
		Connection conn = SK_Config.getConnection();
		return insert(config,conn,tableName);
	}
	
	public static Config insert(Config config,Connection conn,String tableName){
		
		SK_Query sq = new SK_Query();
		String sql = "INSERT INTO " +tableName+ " (id,k,v,remark) VALUES (?,?,?,?)";
		try {
			int i = (int)sq.insert(conn,sql,config.getId(),config.getK(),config.getV(),config.getRemark());
			if(config.getId()==0){
				config.setId(i);
			}
			return i > 0 ? config : null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
	}
	
	public static Config insert(Config config,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insert(config,conn,tableName);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
	
	public static int[] insertBatch(List<Config> configs){
		Connection conn = SK_Config.getConnection();
		return insertBatch(configs,conn);
	}
	
	public static int[] insertBatch(List<Config> configs,Connection conn){
		return insertBatch(configs,conn,Config.TABLENAME);
	}
	
	public static int[] insertBatch(List<Config> configs,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(configs,conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static int[] insertBatch(List<Config> configs,String tableName){
		Connection conn = SK_Config.getConnection();
		return insertBatch(configs,conn,tableName);
	}
	
	public static int[] insertBatch(List<Config> configs,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "INSERT INTO " +tableName+ " (id,k,v,remark) VALUES (?,?,?,?)";
		try {
			int columnSize = 4;
			int size = configs.size();
			Object[][] params = new Object[size][columnSize];
			for (int i = 0; i < size; i++) {
				params[i][0] =configs.get(i).getId();
				params[i][1] =configs.get(i).getK();
				params[i][2] =configs.get(i).getV();
				params[i][3] =configs.get(i).getRemark();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 1 ? is : new int[]{};
		} catch (Exception e) {
			e.printStackTrace();
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
	}
	
	public static int[] insertBatch(List<Config> configs,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(configs,conn,tableName);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
	
	public static Config update(Config config){
		Connection conn = SK_Config.getConnection();
		return update(config,conn);
	}
	
	public static Config update(Config config,Connection conn){
		return update(config,conn,Config.TABLENAME);
	}
	
	public static Config update(Config config,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return update(config,conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Config update(Config config,String tableName){
		Connection conn = SK_Config.getConnection();
		return update(config,conn,tableName);
	}
	
	public static Config update(Config config,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		StringBuffer sb = new StringBuffer();
		Map<String, Object> updateColumns = config.getUpdateColumns();
		int columnSize = updateColumns.size();
		if (updateColumns.isEmpty()) {
			return config;
		}
		sb.append("UPDATE ");
		sb.append(tableName);
		sb.append(" SET ");
		Object[] values = new Object[(columnSize + 1)];
		int i = 0;
		for (Map.Entry<String, Object> updateColumn : updateColumns.entrySet()) {
			String key = updateColumn.getKey();
			values[i] = updateColumn.getValue();
			i++;
			sb.append(key);
			sb.append("=");
			sb.append("?");
			if (i < columnSize) {
				sb.append(",");
			}
		}
		sb.append(" WHERE ");
		sb.append("id");
		sb.append(" = ?");
		values[columnSize] = config.getId();
		String sql = sb.toString();
		try {
			i = run.update(conn, sql, values);			
			return i == 1 ? config : null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally {
			try{
				config.clearUpdateColumn();
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
	}
	
	public static Config update(Config config,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return update(config,conn,tableName);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static int[] updateBatch(List<Config> configs){
		Connection conn = SK_Config.getConnection();
		return updateBatch(configs,conn);
	}
	
	public static int[] updateBatch(List<Config> configs,Connection conn){
		return updateBatch(configs,conn,Config.TABLENAME);
	}
	
	public static int[] updateBatch(List<Config> configs,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(configs,conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static int[] updateBatch(List<Config> configs,String tableName){
		Connection conn = SK_Config.getConnection();
		return updateBatch(configs,conn,tableName);
	}
	
	public static int[] updateBatch(List<Config> configs,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "UPDATE " +tableName+ " SET id = ?,k = ?,v = ?,remark = ? WHERE id = ?";
		try {
			int columnSize = 4;
			int size = configs.size();
			Object[][] params = new Object[size][columnSize + 1];
			for (int i = 0; i < size; i++) {
				params[i][0] =configs.get(i).getId();
				params[i][1] =configs.get(i).getK();
				params[i][2] =configs.get(i).getV();
				params[i][3] =configs.get(i).getRemark();
				params[i][columnSize] =configs.get(i).getId();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 1 ? is : new int[]{};
		} catch (Exception e) {
			e.printStackTrace();
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
	}
	
	public static int[] updateBatch(List<Config> configs,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(configs,conn,tableName);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
	
	public static boolean delete(Config config){
		Connection conn = SK_Config.getConnection();
		return delete(config,conn);
	}
	
	public static boolean delete(Config config,Connection conn){
		return delete(config,conn,Config.TABLENAME);
	}
	
	public static boolean delete(Config config,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return delete(config,conn);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean delete(Config config,String tableName){
		Connection conn = SK_Config.getConnection();
		return delete(config,conn,tableName);
	}
	
	public static boolean delete(Config config,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE id = ?";
		try {
			int i = run.update(conn,sql, config.getId());
			return i > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return false;
			}
		}
	}
	
	public static boolean delete(Config config,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return delete(config,conn,tableName);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	
	public static boolean deleteBatch(List<Config> configs){
		Connection conn = SK_Config.getConnection();
		return deleteBatch(configs,conn);
	}
	
	public static boolean deleteBatch(List<Config> configs,Connection conn){
		return deleteBatch(configs,conn,Config.TABLENAME);
	}
	
	public static boolean deleteBatch(List<Config> configs,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(configs,conn);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean deleteBatch(List<Config> configs,String tableName){
		Connection conn = SK_Config.getConnection();
		return deleteBatch(configs,conn,tableName);
	}
	
	public static boolean deleteBatch(List<Config> configs,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE id = ?";
		try {
			int size = configs.size();
			Object[][] params = new Object[size][1];
			for (int i = 0; i < size; i++) {
				params[i][0] = configs.get(i).getId();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return false;
			}
		}
	}
	
	public static boolean deleteBatch(List<Config> configs,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(configs,conn,tableName);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 根据( id ) 查询
	 */
	public static Config getById(int id){
		Connection conn = SK_Config.getConnection();
		return getById(id, conn);
	}
	
	public static Config getById(int id,String tableName){
		Connection conn = SK_Config.getConnection();
		return getById(id, conn,tableName);
	}
	
	/**
	 * 根据( k ) 查询
	 */
	public static Config getByK(String k){
		Connection conn = SK_Config.getConnection();
		return getByK(k, conn);
	}
	
	public static Config getByK(String k,String tableName){
		Connection conn = SK_Config.getConnection();
		return getByK(k, conn,tableName);
	}
	
	
	//Connection
	/**
	 * 根据( id ) 查询
	 */
	public static Config getById(int id,Connection conn){
		return getById(id,conn,Config.TABLENAME);
	}
	
	public static Config getById(int id,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,k,v,remark FROM " + tableName + " WHERE " + "id = ?";
		Config config = null; 
		try {
			Map<String, Object> map = run.query(conn,sql, new MapHandler(), id);
			config = Config.createForColumnNameMap(map);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
		return config;
	}
	
	/**
	 * 根据( k ) 查询
	 */
	public static Config getByK(String k,Connection conn){
		return getByK(k,conn,Config.TABLENAME);
	}
	
	public static Config getByK(String k,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,k,v,remark FROM " + tableName + " WHERE " + "k = ?";
		Config config = null; 
		try {
			Map<String, Object> map = run.query(conn,sql, new MapHandler(), k);
			config = Config.createForColumnNameMap(map);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
		return config;
	}
	
	
	//DataSource
	/**
	 * 根据( id ) 查询
	 */
	public static Config getById(int id,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getById(id, conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Config getById(int id,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getById(id, conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 根据( k ) 查询
	 */
	public static Config getByK(String k,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getByK(k, conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Config getByK(String k,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getByK(k, conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	
	public static List<Config> getAll(){
		Connection conn = SK_Config.getConnection();
		return getAll(conn);
	}
	
	public static List<Config> getAll(Connection conn){
		return getAll(conn,Config.TABLENAME);
	}
	
	public static List<Config> getAll(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static List<Config> getAll(String tableName){
		Connection conn = SK_Config.getConnection();
		return getAll(conn,tableName);
	}
	
	public static List<Config> getAll(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,k,v,remark FROM " + tableName;
		List<Config> configs = null; 
		try {
			List<Map<String,Object>> list = run.query(conn, sql, new MapListHandler());
			configs = Config.createForColumnNameList(list);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
		return configs;
	}
	
	public static List<Config> getAll(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn,tableName);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
	
	public static List<Config> getAllPage(int page,int pageCount){
		Connection conn = SK_Config.getConnection();
		return getAllPage(conn,page,pageCount);
	}
	
	public static List<Config> getAllPage(Connection conn,int page,int pageCount){
		return getAllPage(conn,Config.TABLENAME,page,pageCount);
	}
	
	public static List<Config> getAllPage(DataSource ds,int page,int pageCount){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,page,pageCount);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static List<Config> getAllPage(String tableName,int page,int pageCount){
		Connection conn = SK_Config.getConnection();
		return getAllPage(conn,tableName,page,pageCount);
	}
	
	public static List<Config> getAllPage(Connection conn,String tableName,int page,int pageCount){
		QueryRunner run = new QueryRunner();
		page = ((page-1) * pageCount);
		String sql = "SELECT id,k,v,remark FROM " + tableName + " LIMIT " + page + " , " +pageCount;
		List<Config> configs = null; 
		try {
			List<Map<String,Object>> list = run.query(conn, sql, new MapListHandler());
			configs = Config.createForColumnNameList(list);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
		return configs;
	}
	
	public static List<Config> getAllPage(DataSource ds,String tableName,int page,int pageCount){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,tableName,page,pageCount);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
}